package com.example.makerspace.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.makerspace.domain.UserDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.enums.UserRoleEnum;
import com.example.makerspace.manager.*;
import com.example.makerspace.service.AdminService;
import com.example.makerspace.utils.JwtUtils;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import static com.example.makerspace.domain.UserDO.ACTIVE_STATUS;


/**
 * @author River
 * @version 1.0
 * @date 2023/10/7 17:05
 * @description:
 */
@Service
public class AdminServiceImpl implements AdminService {

    public final static String OP_ADD = "1";
    public final static String OP_UPDATE = "2";
    public final static String OP_DELETE = "3";

    public static ConcurrentMap<String, BaseManager> managerMap = Maps.newConcurrentMap();
    @Resource
    private ApplicationManager applicationManager;

    @Resource
    private  ApplicationV2Manager applicationV2Manager;
    @Resource
    private UserManager userManager;
    @Resource
    private DemandManager demandManager;
    @Resource
    private TechnicManager technicManager;
    @Resource
    private CompanyManager companyManager;
    @Resource
    private BookManager bookManager;
    @Resource
    private BookChapterManager bookChapterManager;
    @Resource
    private CourseManager courseManager;
    @Resource
    private CourseVideoManager courseVideoManager;
    @Resource
    private ExamManager examManager;
    @Resource
    private NewsManager newsManager;
    @Resource
    private ContactInfoManager contactInfoManager;
    @Resource
    private CategoryManager categoryManager;
    @Resource
    private ResourceManager resourceManager;
    @Resource
    private ExamQuestionManager examQuestionManager;

    @Override
    public Result edit(Map<String, Object> params) {
        String type = MapUtils.getString(params, "type");
        String op = MapUtils.getString(params, "op");
        BaseManager manager = managerMap.get(type);
        switch (op) {
            case OP_ADD:
                return manager.add(params, false);
            case OP_UPDATE:
                return manager.update(params);
            case OP_DELETE:
                return manager.deleteById(params);
        }
        return Result.success();
    }

    @Override
    public PageResult list(PageRequest pageRequest) {
        BaseManager manager = managerMap.get(pageRequest.getType());
        return manager.list(pageRequest);
    }

    @Override
    public Result editDemand(DemandDTO demandDTO) {
        String op = demandDTO.getOp();
        switch (op) {
            case OP_ADD:
                return demandManager.addDemand(demandDTO);
            case OP_UPDATE:
                return demandManager.updateDemand(demandDTO);
            case OP_DELETE:
                return demandManager.deleteById(demandDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result editTechnic(TechnicDTO technicDTO) {
        String op = technicDTO.getOp();
        switch (op) {
            case OP_ADD:
                return technicManager.addTechnic(technicDTO);
            case OP_UPDATE:
                return technicManager.updateTechnic(technicDTO);
            case OP_DELETE:
                return technicManager.deleteById(technicDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result editCompany(CompanyDTO companyDTO) {
        String op = companyDTO.getOp();
        switch (op) {
            case OP_ADD:
                return companyManager.addCompany(companyDTO);
            case OP_UPDATE:
                return companyManager.updateCompany(companyDTO);
            case OP_DELETE:
                return companyManager.deleteById(companyDTO.getId());
        }
        return Result.success();
    }


    @Override
    public Result editUser(UserDTO userDTO, HttpServletRequest request) {
        String op = userDTO.getOp();
        switch (op) {
            case OP_ADD:
                UserDO userDO = new UserDO();
                userDO.setUsername(userDTO.getUsername());
                userDO.setRole(UserRoleEnum.NORMAL.getRole());
                userDO.setPassword(userDTO.getPassword());
                userDO.setNickname(userDTO.getNickname());
                userDO.setStatus(ACTIVE_STATUS);
                return userManager.createUser(userDO);
            case OP_UPDATE:
                String role = JwtUtils.getRole(request);
                return userManager.updateUser(userDTO, role);
            case OP_DELETE:
                return userManager.delete(userDTO);
        }
        return Result.success();
    }

    @Override
    public Result editApplication(ApplicationDTO applicationDTO) {
        String op = applicationDTO.getOp();
        switch (op) {
            case OP_ADD:
                return applicationManager.addApplication(applicationDTO);
            case OP_UPDATE:
                return applicationManager.updateApplication(applicationDTO);
            case OP_DELETE:
                return applicationManager.deleteById(applicationDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result editBook(BookDTO bookDTO) {
        String op = bookDTO.getOp();
        switch (op) {
            case OP_ADD:
                return bookManager.addBook(bookDTO);
            case OP_UPDATE:
                return bookManager.updateBook(bookDTO);
            case OP_DELETE:
                return bookManager.deleteById(bookDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result editBookChapter(BookChapterDTO bookChapterDTO) {
        String op = bookChapterDTO.getOp();
        switch (op) {
            case OP_ADD:
                return bookChapterManager.addBookChapter(bookChapterDTO);
            case OP_UPDATE:
                return bookChapterManager.updateBookChapter(bookChapterDTO);
            case OP_DELETE:
                return bookChapterManager.deleteById(bookChapterDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result bookDetail(BookChapterDTO bookChapterDTO) {
        return bookChapterManager.detail(bookChapterDTO);
    }

    @Override
    public Result editCourse(CourseDTO courseDTO) {
        String op = courseDTO.getOp();
        switch (op) {
            case OP_ADD:
                return courseManager.addCourse(courseDTO);
            case OP_UPDATE:
                return courseManager.updateCourse(courseDTO);
            case OP_DELETE:
                return courseManager.deleteById(courseDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result editCourseVideo(CourseVideoDTO courseVideoDTO) {
        String op = courseVideoDTO.getOp();
        switch (op) {
            case OP_ADD:
                return courseVideoManager.addCourseVideo(courseVideoDTO);
            case OP_UPDATE:
                return courseVideoManager.updateCourseVideo(courseVideoDTO);
            case OP_DELETE:
                return courseVideoManager.deleteById(courseVideoDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result courseDetail(CourseVideoDTO courseVideoDTO) {
        return courseVideoManager.detail(courseVideoDTO);
    }

    @Override
    public JSONObject upload(ResourceDTO resourceDTO) {
        return resourceManager.upload(resourceDTO);
    }

    @Override
    public Result editExamQuestion(ExamQuestionDTO examQuestionDTO) {
        String op = examQuestionDTO.getOp();
        switch (op) {
            case OP_ADD:
                return examQuestionManager.addExamQuestion(examQuestionDTO);
            case OP_UPDATE:
                return examQuestionManager.updateExamQuestion(examQuestionDTO);
            case OP_DELETE:
                return examQuestionManager.deleteById(examQuestionDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result examDetail(ExamQuestionDTO examQuestionDTO) {
        return examQuestionManager.tobDetail(examQuestionDTO);
    }

    @Override
    public Result distinctValues(Map<String, Object> params) {
        String fieldName = MapUtils.getString(params, "fieldName");
        String table = MapUtils.getString(params, "table");
        BaseManager manager = managerMap.get(table);
        if (manager == null) {
            return Result.error(ResultCodeEnum.TABLE_NOT_EXIST);
        }
        Set<String> list = manager.distinctValue(fieldName);
        return Result.success(list);
    }

    @Override
    public Result select(PageRequest pageRequest) {
        Map<String, Object> params = pageRequest.getParams();
        String table = MapUtils.getString(params, "table");
        BaseManager manager = managerMap.get(table);
        if (manager == null) {
            return Result.error(ResultCodeEnum.TABLE_NOT_EXIST);
        }
        return manager.select(pageRequest);
    }


    @Override
    public Result editExam(ExamDTO examDTO) {
        String op = examDTO.getOp();
        switch (op) {
            case OP_ADD:
                return examManager.addExam(examDTO);
            case OP_UPDATE:
                return examManager.updateExam(examDTO);
            case OP_DELETE:
                return examManager.deleteById(examDTO.getId());
        }
        return Result.success();
    }

    @Override
    public PageResult demandList(PageRequest pageRequest) {
        return demandManager.list(pageRequest);
    }

    @Override
    public PageResult technicList(PageRequest pageRequest) {
        return technicManager.list(pageRequest);
    }

    @Override
    public PageResult companyList(PageRequest pageRequest) {
        return companyManager.list(pageRequest);
    }

    @Override
    public PageResult userList(PageRequest pageRequest) {
        return userManager.list(pageRequest);
    }

    @Override
    public PageResult applicationFormList(PageRequest pageRequest) {
        return applicationManager.list(pageRequest);
    }

    @Override
    public PageResult bookList(PageRequest pageRequest) {
        return bookManager.list(pageRequest);
    }

    @Override
    public PageResult courseList(PageRequest pageRequest) {
        return courseManager.list(pageRequest);
    }

    @Override
    public PageResult examList(PageRequest pageRequest) {
        return examManager.list(pageRequest);
    }

    @Override
    public Result editNews(NewsDTO newsDTO) {
        String op = newsDTO.getOp();
        switch (op) {
            case OP_ADD:
                return newsManager.addNews(newsDTO);
            case OP_UPDATE:
                return newsManager.updateNews(newsDTO);
            case OP_DELETE:
                return newsManager.deleteById(newsDTO.getId());
        }
        return Result.success();
    }


    @Override
    public PageResult newList(PageRequest pageRequest) {
        return newsManager.list(pageRequest);
    }

    @Override
    public Result editContactInfo(ContactInfoDTO contactInfoDTO) {
        String op = contactInfoDTO.getOp();
        switch (op) {
            case OP_ADD:
                return contactInfoManager.addContactInfo(contactInfoDTO);
            case OP_UPDATE:
                return contactInfoManager.updateContactInfo(contactInfoDTO);
            case OP_DELETE:
                return contactInfoManager.deleteById(contactInfoDTO.getId());
        }
        return Result.success();
    }

    @Override
    public PageResult contactInfoList(PageRequest pageRequest) {
        return contactInfoManager.list(pageRequest);
    }

    @Override
    public Result editCategory(CategoryDTO categoryDTO) {
        String op = categoryDTO.getOp();
        switch (op) {
            case OP_ADD:
                return categoryManager.addCategory(categoryDTO);
            case OP_UPDATE:
                return categoryManager.updateCategory(categoryDTO);
            case OP_DELETE:
                return categoryManager.deleteById(categoryDTO.getId());
        }
        return Result.success();
    }

    @Override
    public Result categoryList(PageRequest pageRequest) {
        return categoryManager.list(pageRequest);
    }


    @Override
    public Result deleteAttachment(ApplicationDTO applicationDTO) {
        return applicationManager.deleteAttachment(applicationDTO);
    }

    @Override
    public Result addApplication(ApplicationV2DTO applicationV2DTO) {
        return applicationV2Manager.addRegistration(applicationV2DTO);
    }

    @Override
    public Result updateApplication(ApplicationV2DTO applicationV2DTO) {
        return applicationV2Manager.updateApplication(applicationV2DTO);
    }
    @Override
    public Result deleteApplication(Integer id, String username, Integer competitionId) {
        return applicationV2Manager.deleteApplication(id,username,competitionId);
    }

    @Override
    public Result listDemand(PageRequest pageRequest) {
        return applicationV2Manager.listDemand(pageRequest);
    }
    @Override
    public Result deleteUser(UserDTO userDTO) {
        return applicationV2Manager.deleteUser(userDTO);
    }

    @Override
    public Result deleteByCompetitionId(ApplicationV2DTO applicationV2DTO) {
        return applicationV2Manager.deleteByCompetitionId(applicationV2DTO);
    }

    @Override
    public Result listUser(PageRequest pageRequest) {
        return applicationV2Manager.listUser(pageRequest);
    }

    @Override
    public Result showAuthentication(String username) {
        return applicationV2Manager.showAuthentication(username);
    }

    @Override
    public Result showUserList(PageRequest pageRequest) {
        return applicationV2Manager.showUserList(pageRequest);
    }

    @Override
    public Result editUserList(UserDTO userDTO) {
        return applicationV2Manager.editUserList(userDTO);
    }

    @Override
    public Result resetPassword(UserDTO userDTO) {
        return applicationV2Manager.resetPassword(userDTO);
    }

    @Override
    public Result onlineUser(PageRequest pageRequest) {
        return applicationV2Manager.onlineUser(pageRequest);
    }

    @Override
    public Result addUser(UserDTO userDTO) {
        return applicationV2Manager.addUser(userDTO);
    }

    @Override
    public Result updateUserState(UserDTO userDTO) {
        return applicationV2Manager.updateUserState(userDTO);
    }

    @Override
    public Result assignPermissions(UserDTO userDTO) {
        return applicationV2Manager.assignPermissions(userDTO);
    }

    @Override
    public Result userRegistration() {
        return applicationV2Manager.userRegistration();
    }


    @Override
    public Result entRealName() {
        return applicationV2Manager.entRealName();
    }

    @Override
    public Result questionsRegistration() {
        return applicationV2Manager.questionsRegistration2();
    }

    @Override
    public Result showProducts() {
        return applicationV2Manager.showProducts2();
    }



    @Override
    public Result pointSort() {
        return applicationV2Manager.pointSort2();
    }

    @Override
    public Result showTableData() {
        return applicationV2Manager.showTableData();
    }

    @Override
    public Result otherShow() {
        return applicationV2Manager.otherShow();
    }

    @Override
    public Result forcedOffline(UserDTO userDTO) {
        return applicationV2Manager.forcedOffline(userDTO);
    }

    @Override
    public Result listProducts(PageRequest pageRequest) {
        return applicationV2Manager.listProducts2(pageRequest);
    }

    @Override
    public Result updateProducts(DemandDTO demandDTO) {
        return applicationV2Manager.adminUpdateProducts(demandDTO);
    }



    @Override
    public Result showUserOperate(PageRequest pageRequest) {
        return applicationV2Manager.showUserOperate(pageRequest);
    }

    @Override
    public Result detailProduct(Integer id, Integer type) {
        return applicationV2Manager.detailProduct(id,type);
    }

    @Override
    public Result deleteProducts(Integer id) {
        return applicationV2Manager.deleteProducts(id);
    }

    @Override
    public Result personalName(PageRequest pageRequest) {
        return applicationV2Manager.personalName(pageRequest);
    }

    @Override
    public Result entAuthentication(PageRequest pageRequest) {
        return applicationV2Manager.entAuthentication(pageRequest);
    }

    @Override
    public Result newBulletin(PageRequest pageRequest) {
        return applicationV2Manager.newBulletin(pageRequest);
    }

    @Override
    public Result addBulletin(NewsDTO newsDTO) {
        return applicationV2Manager.addBulletin(newsDTO);
    }

    @Override
    public Result updateNewBulletin(NewsDTO newsDTO) {
        return applicationV2Manager.updateNewBulletin(newsDTO);
    }

    @Override
    public Result deleteNewBulletin(String id) {
        return applicationV2Manager.deleteNewBulletin(id);
    }

    @Override
    public Result entDetail(String name) {
        return applicationV2Manager.entDetail2(name);
    }

    @Override
    public Result submission(PageRequest pageRequest) {
        return applicationV2Manager.submission(pageRequest);
    }

    @Override
    public Result entData(PageRequest pageRequest) {
        return applicationV2Manager.entData(pageRequest);
    }

    @Override
    public Result showProductHistory(Integer productId) {
        return applicationV2Manager.showProductHistory(productId);
    }
}
